package com.tanhua.server.service;

import com.tanhua.common.pojo.User;
import com.tanhua.common.service.SettingsService;
import com.tanhua.common.utils.UserThreadLocal;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
@Slf4j
public class UserService {
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private SettingsService settingsService;
    @Value("${tanhua.sso.url}")
    private String ssoUrl;


    public User queryUserByToken(String token) {
        String url = ssoUrl+ "/user/" + token;
        try {
            User user = restTemplate.getForObject(url, User.class);
            if (user==null) {
                return null;
            }
            return user;
        } catch (Exception e) {
            log.error("token错误,token:" + token, e);
            System.out.println(e);
        }
        return null;
    }

    public void updateNotification(Boolean likeNotification, Boolean pinglunNotification, Boolean gonggaoNotification) {
        User user = UserThreadLocal.get();
        this.settingsService.updateNotification(user.getId(), likeNotification, pinglunNotification, gonggaoNotification);
    }
}
